//
//  XDHomeAttainmentViewController.m
//  XDEducation
//
//  Created by apple on 2017/4/7.
//  Copyright © 2017年 apple. All rights reserved.
//

#import "XDHomeAttainmentViewController.h"
#import "XDCollectionItemCell.h"
#import "XDAttainmentData.h"
#import "XDAttainmentBtnCell.h"
#import "XDHomeVideoCell.h"
#import "XDVideoModel.h"
#import "XDHomeCellHeaderView.h"
#import "XDVideoPlayViewController.h"
#import "WebViewController.h"
#import "XDAvenueOfStarsController.h"
#import "XDGradeVideoController.h"

@interface XDHomeAttainmentViewController ()

/** 页面布局数据 */
@property(nonatomic, strong)  NSMutableArray *attainmentData;

/** 好成绩视频数据源 */
@property(nonatomic,strong) NSMutableArray <XDVideoModel *>*videoModelArr;

@end

static NSString *const btnCellId = @"BtnCell";
static NSString *const viedoCellId = @"ViedoCell";

@implementation XDHomeAttainmentViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    
    self.tableView.frame = CGRectMake(0, 44, ScreenW, ScreenH - 44 - 64 - 44);
    [self initNetworkingWithRefreshStatus:RefreshStatePullDown];
}


#pragma mark - 懒加载
-(NSMutableArray *)attainmentData{

    if (_attainmentData == nil) {
        
        _attainmentData = [NSMutableArray array];
        _attainmentData = [XDAttainmentData getAttainmentData];
    }
    return _attainmentData;
}

#pragma mark - tableview delegate
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    
    return 4;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{

    return 1;
}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    
    return 5;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    switch (indexPath.section) {
        case 0:
            return 270 * ScaleHeight;
            break;
        case 1:
            return 100 * ScaleHeight;
            break;
        case 2:
            return 100 * ScaleHeight;
            break;
        case 3:
            return 170 * ScaleHeight;
            break;
            
        default:
            return 0;
            break;
    }
    
}

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{

    if (section == 2)
        return nil;
    
    NSString *title = self.attainmentData[section][@"title"];
    
    BOOL isShowMoreBtn = NO;
    
    if (section == 3) {
        
        isShowMoreBtn = YES;
    }
    
    XDHomeCellHeaderView *headerView =  [[XDHomeCellHeaderView alloc]initWithTitle:title MoreBtnStatus:isShowMoreBtn];
    headerView.moreBlock = ^(){
        
        if ([title isEqualToString:@"好素养视频"])
        {
            XDGradeVideoController *VC = [[XDGradeVideoController alloc] init];
            VC.XDGradeVideoControllerType = HaoSuYang;
            VC.title = title;
            [self.navigationController pushViewController:VC animated:YES];
        }
        
    };
    return headerView;
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{

    if (section == 2) return 0;
    return 44 * ScaleHeight;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

    
    switch (indexPath.section) {
        case 0:{
            
            NSArray *itemArr = self.attainmentData[0][@"item"];
            
            XDCollectionItemCell *cell = [XDCollectionItemCell cellItemArray:itemArr tableView:tableView];
            
            [cell setItemPushBlock:^(NSString *nameTitle) {
                
                if ([nameTitle isEqualToString:@"棋行天下"]) {
                    NSString *str = [[NSString alloc] initWithFormat:@"%@",Base@"bbsws.do?action=showalltopic&forumcode=XXDD_FDGG_XQAH_CHESSAIHAO&pageNumber=1"];
                    [self theURL:str title:nameTitle];
                    
                }else if ([nameTitle isEqualToString:@"球星之路"]){
                    NSString *str = [[NSString alloc] initWithFormat:@"%@",Base@"bbsws.do?action=showalltopic&forumcode=XXDD_FDGG_XQAH_QLAH&pageNumber=1"];
                    [self theURL:str title:nameTitle];
                    
                }else if ([nameTitle isEqualToString:@"智慧巧手"]){
                    NSString *str = [[NSString alloc] initWithFormat:@"%@",Base@"bbsws.do?action=showalltopic&forumcode=XXDD_FDGG_XQAH_SGAH&pageNumber=1"];
                    [self theURL:str title:nameTitle];
                    
                }else if ([nameTitle isEqualToString:@"经典引荐"]){
                    NSString *str = [[NSString alloc] initWithFormat:@"%@",Base@"bbsws.do?action=showalltopic&forumcode=XXDD_FDGG_XQAH_TJSJ&pageNumber=1"];
                    [self theURL:str title:nameTitle];
                    
                }else if ([nameTitle isEqualToString:@"小摄影师"]){
                    NSString *str = [[NSString alloc] initWithFormat:@"%@",Base@"bbsws.do?action=showalltopic&forumcode=XXDD_FDGG_XQAH_YQAH&pageNumber=1"];
                    [self theURL:str title:nameTitle];
                    
                }else if ([nameTitle isEqualToString:@"弦乐之音"]){
                    NSString *str = [[NSString alloc] initWithFormat:@"%@",Base@"bbsws.do?action=showalltopic&forumcode=XXDD_FDGG_XQAH_XXSYS&pageNumber=1"];
                    [self theURL:str title:nameTitle];
                    
                }else if ([nameTitle isEqualToString:@"爱的美食"]){
                    NSString *str = [[NSString alloc] initWithFormat:@"%@",Base@"bbsws.do?action=showalltopic&forumcode=XXDD_FDGG_XQAH_ADMS&pageNumber=1"];
                    [self theURL:str title:nameTitle];
                    
                }else if ([nameTitle isEqualToString:@"绘画小屋"]){
                    NSString *str = [[NSString alloc] initWithFormat:@"%@",Base@"bbsws.do?action=showalltopic&forumcode=XXDD_FDGG_XQAH_HHXW&pageNumber=1"];
                    [self theURL:str title:nameTitle];
                    
                }else if ([nameTitle isEqualToString:@"书法天地"]){
                    NSString *str = [[NSString alloc] initWithFormat:@"%@",Base@"bbsws.do?action=showalltopic&forumcode=XXDD_FDGG_XQAH_SFTD&pageNumber=1"];
                    [self theURL:str title:nameTitle];
                    
                }else if ([nameTitle isEqualToString:@"炫舞精灵"]){
                    NSString *str = [[NSString alloc] initWithFormat:@"%@",Base@"bbsws.do?action=showalltopic&forumcode=XXDD_FDGG_XQAH_XWJL&pageNumber=1"];
                    [self theURL:str title:nameTitle];
                    
                    
                }
                
                
                
            }];
            
            
            
            return cell;
            
        }
            break;
         
        case 1:{
        
            NSArray *itemArr = self.attainmentData[1][@"item"];
            
            XDCollectionItemCell *cell = [XDCollectionItemCell cellItemArray:itemArr tableView:tableView];
            
            [cell setItemPushBlock:^(NSString *nameTitle) {
                
                if ([nameTitle isEqualToString:@"手工制作"]) {
                    NSString *str = [[NSString alloc] initWithFormat:@"%@",Base@"bbsws.do?action=showalltopic&forumcode=XXDD_FDGG_XQAH_XWJL&pageNumber=1"];
                    [self theURL:str title:nameTitle];

                    
                }else if ([nameTitle isEqualToString:@"判断力"]){
                    NSString *str = [[NSString alloc] initWithFormat:@"%@",Base@"bbsws.do?action=showalltopic&forumcode=XXDD_QDLY_TZXL_PDL&pageNumber=1"];
                    [self theURL:str title:nameTitle];

                    
                }else if ([nameTitle isEqualToString:@"注意力"]){
                    NSString *str = [[NSString alloc] initWithFormat:@"%@",Base@"bbsws.do?action=showalltopic&forumcode=XXDD_QDLY_TZXL_ZYL&pageNumber=1"];
                    [self theURL:str title:nameTitle];

                    
                }else if ([nameTitle isEqualToString:@"思考力"]){
                    NSString *str = [[NSString alloc] initWithFormat:@"%@",Base@"bbsws.do?action=showalltopic&forumcode=XXDD_QDLY_TZXL_SKL&pageNumber=1"];
                    [self theURL:str title:nameTitle];

                    
                }
                
                
            }];
            
            
            
            
            
            return cell;
        }
            break;
        case 2:{
        
            XDAttainmentBtnCell *cell = [tableView dequeueReusableCellWithIdentifier:btnCellId];
            
            if (!cell) {
                
                [tableView registerNib:[UINib nibWithNibName:@"XDAttainmentBtnCell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:btnCellId];
                cell = [tableView dequeueReusableCellWithIdentifier:btnCellId];
            }
            
            [cell setItemPushBlock:^(NSString *nameTitle) {
                
                if ([nameTitle isEqualToString:@"星光大道"]) {
                    XDAvenueOfStarsController *avenue = [XDAvenueOfStarsController new];
                    avenue.nameTitle = nameTitle;
                    [self.navigationController pushViewController:avenue animated:YES];
                }else if ([nameTitle isEqualToString:@"奇思妙想"]){
                    
                
                        
                        
                        NSString *url = BaseURL(@"user.do?action=countClicks");
                        NSDictionary *dict = @{@"uid":@"285",@"funcCode":@"37"};
                        [XDHttpTool get:url authCode:[AccountTool shareAccountTool].currentAccount.authCode params:dict success:^(id responseObj) {
                            
                        } failure:^(NSError *error) {
                            
                        }];
                        
              
                    NSString *str = [[NSString alloc] initWithFormat:@"%@",Base@"bbsws.do?action=showalltopic&forumcode=XXDD_FDGG_QSMX&pageNumber=1"];
                    [self theURL:str title:nameTitle];
                }
               
                
            }];
            
            
            
            return cell;
        }
            break;
            
        case 3:{
                
            [tableView registerNib:[UINib nibWithNibName:@"XDHomeVideoCell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:viedoCellId];
            
            XDHomeVideoCell *cell = [tableView dequeueReusableCellWithIdentifier:viedoCellId];
            cell.videoArr = self.videoModelArr;
            WeakSelf(weakself)
            cell.videoClickedBlock = ^(XDVideoModel *model){
                if (model.absoluteUrl.length == 0) {
                    return ;
                }
                XDVideoPlayViewController *vc = [[XDVideoPlayViewController alloc]init];
                vc.videoModel = model;
                [weakself presentViewController:vc animated:YES completion:nil];
            };
            return cell;
        }
            break;
        default:
            return nil;
            break;
    }
    
    
   ;
}


-(void)initNetworkingWithRefreshStatus:(RefreshState)status{
    
    //media.do
    //[page=1 | action=videolist | vtype=2 | type=0]
    
    //vtype=0 好成绩  vtype=1好习惯  vtype=2 好素养
    NSString *url = BaseURL(@"media.do?action=videolist");
    NSDictionary *dict = @{@"type":@0,@"vtype":@2,@"page":@1};
    [XDHttpTool get:url authCode:[AccountTool shareAccountTool].currentAccount.authCode params:dict success:^(id responseObj) {
        
        //NSLog(@"%@",responseObj);
        NSArray *dictArr = [responseObj objectForKey:@"dataList"];
        NSMutableArray *modelArr = [XDVideoModel mj_objectArrayWithKeyValuesArray:dictArr];
        self.videoModelArr = modelArr;
        [self.tableView reloadData];
        
    } failure:^(NSError *error) {
        
        NSLog(@"%@",error);
    }];
    
}


- (void)theURL:(NSString *)str title:(NSString *)title{
    
    WebViewController *web = [WebViewController new];
    
    web.str = str;
    web.nameTitle = title;
    [self.navigationController pushViewController:web animated:YES];
}




@end
